<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--图书类型数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="typeName"
            placeholder="请输入图书类型名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="bookTypeOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="typehandleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="图书名称" prop="bookName">
            <el-input
              v-model="queryParams.bookName"
              placeholder="请输入图书名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="图书作者" prop="bookAuthor">
            <el-input
              v-model="queryParams.bookAuthor"
              placeholder="请输入图书作者"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:book:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:book:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:book:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              :loading="exportLoading"
              @click="handleExport"
              v-hasPermi="['system:book:export']"
              >导出</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-download"
              size="mini"
              :disabled="multiple"
              :loading="exportLoading"
              @click="handlegostatus"
              v-hasPermi="['system:book:gostatus']"
              >出库</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="bookList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="图书编号" align="center" prop="bookId" />
          <el-table-column label="图书名称" align="center" prop="bookName" />
          <el-table-column label="图书作者" align="center" prop="bookAuthor" />
          <el-table-column label="图书类型" align="center" prop="type.typeName" />
          <el-table-column
            label="状态"
            align="center"
            prop="bookStatus"
            :formatter="statusFormatter"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
          <!-- <el-table-column label="图书类型" align="center" prop="bookType" /> -->
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:book:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:book:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改图书对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="归属图书类型" prop="typeId">
              <treeselect v-model="form.bookType" :options="bookTypeOptions1" :show-count="true" placeholder="请选择图书类型" />
            </el-form-item>

        <el-form-item label="图书名称" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入图书名称" />
        </el-form-item>
        <el-form-item label="图书作者" prop="bookAuthor">
          <el-input v-model="form.bookAuthor" placeholder="请输入图书作者" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBook,
  getBook,
  delBook,
  addBook,
  updateBook,
  exportBook,
  statusBook,
  
} from "@/api/system/book";
import editTable from "../../tool/gen/editTable.vue";
import {listType}from "@/api/system/type";

import {treeselect} from "@/api/system/type"
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  components: { editTable },
  name: "Book",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部门名称
      typeName: "",
      // 图书表格数据
      bookList: [],
      // 弹出层标题
      title: "",
      //图书类型树选项
      bookTypeOptions: [],
      bookTypeOptions1: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookName: null,
        bookAuthor: null,
        bookId: null,
        bookType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      defaultProps: {
        children: "children",
        label: "typeName"
      },
      rules: {
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    typeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTypeList();
    //this.getTreeselect();
  },
  methods: {
    // 出库
    handlegostatus(row) {
      console.log("出库");
      const bookIds = row.bookId || this.ids;

      this.$modal
        .confirm('是否确认出库图书编号为"' + bookIds + '"的数据项？')
        .then(function () {
          return statusBook(bookIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("出库成功");
        })
        .catch(() => {});
    },

    statusFormatter(row, column) {
      const status = row.bookStatus;
      if (status == 0) {
        return "正常";
      } else if (status == 1) {
        return "出库";
      } else if (status == 2) {
        return "入库";
      }
    },
    /** 查询图书类型列表 */
    getTypeList() {
      this.loading = true;
      listType(this.queryParams).then(response => {
        this.bookTypeOptions = this.handleTree(response.data, "typeId", "parentId");
        this.loading = false;
      });
    },
    /** 查询图书列表 */
    getList() {
      this.loading = true;
      listBook(this.queryParams).then((response) => {
        this.bookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bookId: null,
        bookName: null,
        bookAuthor: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        bookStatus: "0",
        bookType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bookId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

      /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.bookTypeOptions1 = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.typeName.indexOf(value) !== -1;
    },
    // 节点单击事件
    typehandleNodeClick(data) {
      console.log(data);
      this.queryParams.bookType = data.typeId;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
      this.open = true;
      this.title = "添加图书";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
       this.getTreeselect();
      const bookId = row.bookId || this.ids;
      getBook(bookId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改图书";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bookId != null) {
            updateBook(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBook(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bookIds = row.bookId || this.ids;
      this.$modal
        .confirm('是否确认删除图书编号为"' + bookIds + '"的数据项？')
        .then(function () {
          return delBook(bookIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有图书数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportBook(queryParams);
        })
        .then((response) => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
